package Server;

import java.util.List;
import java.util.Vector;

public class Silhouette {
	private static double euclideanDistance(double[] data, double[] center) {  
        if(data.length == 0 || data == null || center.length == 0 || center == null) {  
            return 0.0;  
        } else if(center.length != data.length) {  
            throw new RuntimeException("执行的时候数据长度和中心长度不一致。");  
        }  
          
        double sum = 0.0;  
          
        for(int i = 0; i < data.length; i++) {  
            sum += Math.pow(data[i] - center[i] , 2);  
        }  
          
        return Math.sqrt(sum);  
              
    }  
	
	
	private static double silhouetteCoefficient(List<Vector> data, List<Integer> result, int flag, Vector center, int centerSize) {  
        double sameClusterSum = 0.0;  
        double otherClusterSum = 0.0;  
        double min = Double.MAX_VALUE;  
          
        for(int j = 0; j < centerSize; j++) {  
            if(j != flag) {  
                for(int i = 0; i < data.size(); i++) {  
                    if(result.get(i) == j) {  
                        otherClusterSum += euclideanDistance(data.get(i).toArray(), center.toArray());  
                    }  
                }  
                min = min<otherClusterSum ? min:otherClusterSum; //非同一cluster里面的最短distance  
            }  
        }  
          
        for(int i = 0; i < data.size(); i++) {  
            if(result.get(i)==flag) {  
                sameClusterSum += euclideanDistance(data.get(i).toArray(), center.toArray());  
            }   
        }  
          
        double coef = (min/data.size() - sameClusterSum/data.size()) / Math.max(sameClusterSum/data.size(), min/data.size());  
        return coef;  
    }  
}
